<?php

    require_once(dirname(__FILE__).'/global.inc');

    if(isset($_GET['speccode']))
    {
        $mikron->Cache->setCache('speccode', $_GET['speccode']);
    }

    $pageindex = 3;
    $postid = $mikron->Cache->getCache('speccode');
    $prevpage = '/infomat/select_spec.php';
    $nextpage = '/infomat/result.php';
    $nextbutton_action = 'checkcode();';

    require_once(dirname(__FILE__).'/schedule.inc');
    LoadShedules($postid, $datebegin, $dateend);

    function FreeTicketsCountForDay($day, $postid)
    {
        global $ched, $postid;
        foreach($ched->items() as $item)
        {
            if($item->id == $postid)
            {
                return $item->datetimelist->FreeTicketsCount($day);
            }
        }
    }
?>
<table border="0" valign="top" cellspacing="0" cellpaddinf="0" width="100%" height="100%">
<TR>
<TD valign="top" width="90%">

    <table border="0" valign="top" width="100%" cellspacing="0" cellpadding="0"> 
    <TR>
        <TD width="20">

                <DIV 
                    onclick="movescroll('scroll_holder', -1);"
                    ondblclick="movescroll('scroll_holder', -1);"        
                    onmousedown="javascript: m_down(this, 'left');"
                    onmouseup="javascript: m_up(this, 'left');"
                    onmouseout="javascript: m_up(this, 'left');"
                    style="width: 32px; height: 80px; background-image: url(<?php echo constant('SM_TEMPLATE_WWW');?>images/v_arrow_left.png);"></DIV>

        </TD>
        <TD width="*" style="overflow: auto; padding-bottom: 0.5em;">
            <table border="0" width="100%" cellspacing="0" cellpadding="0"> 
            <TR>
                    <?php
                    for($i=0;$i<$schedule_days;$i++)
                    {
                        addDayButton($i, date('Y-m-d', DateAdd("d", $i, time())));
                    }
                    function addDayButton($caption, $day)
                    {
                        global $postid;
                        $freeticketscount = FreeTicketsCountForDay($day, $postid);
                        ?>
                        <TD class="ticket">
                            <table align="center" class="tickettable"
                                    onmousedown="javascript: day_down(this); selectday(this, '<?php echo $day;?>');"
                                   id="<?php echo $day;?>">
                            <TR>
                                <TD class="cellday"><?php echo beautifulDate(strtotime($day), false, true);?></TD>
                                <TD class="cellweekdayname" width="30"><?php echo BeautifulWeekDayName(strtotime($day));?></TD>
                            <TR>
                                <TD class="celltickets">
                                    талонов
                                </TD>
                                <TD class="cellticketscount">
                                    <?php if($freeticketscount>0){echo $freeticketscount;}else{echo "&times;";}?>
                                </TD>                                
                            </TR>
                            </table>
                        </TD>
                        <?php
                    }                    
                    ?>
            </TR>
            </TABLE>
        </TD>
        <TD width="20" >
                <DIV 
                    onclick="movescroll('scroll_holder', -1);"
                    ondblclick="movescroll('scroll_holder', -1);"        
                    onmousedown="javascript: m_down(this, 'right');"
                    onmouseup="javascript: m_up(this, 'right');"
                    onmouseout="javascript: m_up(this, 'right');"
                    style="width: 32px; height: 80px; background-image: url(<?php echo constant('SM_TEMPLATE_WWW');?>images/v_arrow_right.png);"></DIV>

        </TD>
    </TR>
    <TR>
        <TD colspan="3" a lign="center">
            <?php
                // Вывод талончиков врачей на выбранный день
                foreach($ched->items() as $item)
                {
                    if($item->id == $postid)
                    {
                        ?><!-- ------------------------------------------------------------------- --><?php
                        $prevdate = "";
                        foreach($item->datetimelist->items() as $dtl)
                        {
                            if($prevdate=="" || $prevdate != $dtl->date)
                            {
                                if($prevdate!=""){?></TABLE><?php }
                                $prevdate = $dtl->date;
                                $cnt=0;
                                ?><TABLE cellpadding="0" id="dt<?php echo $dtl->date;?>" style="display: none;"><?php
                            }
                            $color = "#85c325";
                            if($cnt%$shedule_time_row==0)
                            {
                                if($prevdate!=""){?></TD><?php }
                                ?><TD><?php
                            }
                            if($dtl->status==1){$color="#cd430e";}
                            ?><TABLE width="100%" height="100%" cell padding="18" 
                                     border="0"><TR>
                                     <TD <?php if($dtl->status==0){?>onmousedown="selecttime(this,'<?php echo $dtl->time;?>', '<?php echo $dtl->id;?>', '<?php echo $dtl->place;?>');"<?}?>
                                style="background-color: <?php echo $color?>;" class="timeticket"><?php
                            echo $dtl->time,"<BR>";
                            $cnt++;
                            ?></TD></TR></TABLE><?php
                        }
                        if($prevdate!=""){?></TD></TR></TABLE><?}
                        ?><!-- ------------------------------------------------------------------- --><?php
                    }
                }
            ?>
        </TD>
    </TR>
    </TABLE>
</TD>
<TD style="padding-left: .5em; ">
    <?php require_once(dirname(__FILE__).'/help.inc');
    require_once(dirname(__FILE__).'/patient.inc');
    if(function_exists('showpacientinfo'))
    {
        showpacientinfo();
    }
    ?>
</td>
</TR>
</table>

<script type="text/javascript">

    var selectedday = "";
    var selectedday_value = "";
    var selectedtime = "";
    var selected_time_item = "";
    var button_sel = "";
    var ticketid = "";
    var ticket_place = "";

    function updaterecordto()
    {
        var recordto_element = document.getElementById("recordto");
        if(recordto_element != null)
        {
            if(selectedtime=="")
            {
                recordto_element.innerHTML = "";
            }
            else
            {
                recordto_element.innerHTML = "на " + selectedday_value+" "+selectedtime;
            }
        }
    }
    
    function selectday(button, day)
    {
        // кнопочки дней
        if(button_sel!=""&&button_sel!=null)
        {
            button_sel.style.backgroundPosition = "left -192px";
        }
        button.style.backgroundPosition = "-103px -192px";
        button_sel = button;

        // выбранная дата
        selectedday_value = day;
        if((selectedday != null) && (selectedday!=""))
        {
            selectedday.style.display = 'none';
        }
        selectedday = document.getElementById("dt"+day);
        if(selectedday != null)
        {
            selectedday.style.display = '';
        }
        else
        {
        
        }
        selectedtime = "";
        if(selected_time_item != "")
        {
            selected_time_item.style.backgroundColor = '#85c325';
        }
        selected_time_item = "";
        updaterecordto();
    }

    function selecttime(button, xtime, guidTcketID, place)
    {
        if(xtime == selectedtime)
        {
            button.style.backgroundColor = '#85c325';
            selectedtime = "";
            ticketid = "";
        }
        else
        {
            if (selectedtime != "")
            {
                selected_time_item.style.backgroundColor = '#85c325';
            }
            button.style.backgroundColor = '#0692e1';
            ticketid = guidTcketID;
            ticket_place = place;
            selectedtime = xtime;
            selected_time_item = button
        }
        updaterecordto();
    }
    
    function FillShedule(day,code,speccode)
    {
        xalert('Пациент ID=' + code + ' хочет записаться на ' + day + '<br>на прием к врачу ID=' + speccode);
    }
    
    function checkcode()
    {
        var checked = true;
        if(selectedday_value=="")
        {
            checked = false;
        }
        if(selectedtime=="")
        {
            checked = false;
        }
        if(checked)
        {
            okcode();
        }
        else
        {
            badcode();
        }        
    }
    
    function okcode()
    {
        xalert2("ПОДОЖДИТЕ...");
        <?php $post = $ched->itembyid($postid);?>
        var adr = "<?php echo $nextpage;?>";
        adr += "?date=" + selectedday_value;
        adr += "&time=" + selectedtime;
        adr += "&postspeciality=" + "<?php echo $post->speciality;?>";
        adr += "&postfio=" + "<?php echo $post->fio;?>";
        adr += "&postdivision=" + "<?php echo $post->division;?>";
        adr += "&place=" + ticket_place;
        adr += "&ticketid=" + ticketid;
        location.href = adr;
    }
    
    function badcode()
    {
        xalert('Выберите день и время, на которые Вы желаете записаться!');
    }

    function movescroll(id, toside)
    {
        var obj = document.getElementById(id);
        obj.scrollTop += 28 * toside;
    }

    function day_up(obj)
    {
    }

    function day_down(obj)
    {
    }

    function m_up(obj, id)
    {
        obj.style.backgroundImage = "url(<?php echo constant('SM_TEMPLATE_WWW');?>images/v_arrow_"+id+".png)";
    }

    function m_down(obj, id)
    {
        obj.style.backgroundImage = "url(<?php echo constant('SM_TEMPLATE_WWW');?>images/v_arrow_"+id+"_p.png)";    
    }

</script>

<?php $mikron->Page->EndBody(); ?>